package io.github.generator.strategy;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.generator.config.InjectionConfig;
import io.github.common.consts.Version;
import io.github.common.util.StringUtils;
import io.github.generator.CodeGenerator;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

/**
 * <p>Company: 深圳振瀚信息技术有限公司成都分公司 </p>
 * <p>Description: </p>
 *
 * @author haijun
 * @version 1.0.0
 * @email "mailto:haijun@qq.com"
 * @date 2024.11.09 17:51
 * @since 1.0.0
 */
public class InjectionConfigStrategy implements Strategy<CodeGenerator, InjectionConfig.Builder> {

    /**
     * Customize
     */
    private Consumer<InjectionConfig.Builder> customize;

    /**
     * Injection config strategy
     *
     * @since 1.0.0
     */
    public InjectionConfigStrategy() {
    }

    /**
     * Injection config strategy
     *
     * @param customize customize
     * @since 1.0.0
     */
    public InjectionConfigStrategy(Consumer<InjectionConfig.Builder> customize) {
        this.customize = customize;
    }

    /**
     * Builder
     *
     * @param generator generator
     * @param builder   builder
     * @since 1.0.0
     */
    @Override
    public void builder(CodeGenerator generator, InjectionConfig.Builder builder) {
        builder.customMap(this.customMap(generator));
        if (this.customize != null) {
            this.customize.accept(builder);
        }
    }

    /**
     * Custom map
     *
     * @param generator generator
     * @return the map
     * @since 1.0.0
     */
    private Map<String, Object> customMap(CodeGenerator generator) {
        Map<String, Object> map = new HashMap<>();
        map.put("datetime", DateUtil.now());
        map.put("version", Version.VERSION);
        map.put("since", Version.VERSION);
        map.put("excludedFields", generator.getExcludedFields());
        map.putAll(this.buildFileParams(generator));
        return map;
    }

    /**
     * Build file params
     *
     * @param generator generator
     * @return the map
     * @since 1.0.0
     */
    private Map<String, Object> buildFileParams(CodeGenerator generator) {
        Map<String, Object> map = new HashMap<>();
        //req
        map.put("baseReq", generator.getBaseReq().getSimpleName());
        map.put("baseReqClz", generator.getBaseReq().getName());
        map.put("baseReqLower", StringUtils.toRootLowerCase(generator.getBaseReq().getName()));
        map.put("baseReqPackage", generator.getBaseReq().getPackage().getName());

        //form
        map.put("baseForm", generator.getBaseForm().getSimpleName());
        map.put("baseFormClz", generator.getBaseForm().getName());
        map.put("baseFormLower", StringUtils.toRootLowerCase(generator.getBaseForm().getName()));
        map.put("baseFormPackage", generator.getBaseForm().getPackage().getName());

        //dto
        map.put("baseDto", generator.getBaseDTO().getSimpleName());
        map.put("baseDtoClz", generator.getBaseDTO().getName());
        map.put("baseDtoLower", StringUtils.toRootLowerCase(generator.getBaseDTO().getName()));
        map.put("baseDtoPackage", generator.getBaseDTO().getPackage().getName());

        //entity
        map.put("baseEntity", generator.getBaseDTO().getSimpleName());
        map.put("baseEntityClz", generator.getBaseDTO().getName());

        return map;
    }
}
